<template>
  <div class='login-container'>
    <!-- 顶部导航栏 -->
    <van-nav-bar id="page-nav-bar" title="登录">
      <van-icon name="arrow-left" slot="left" size="22" @click="$router.back()" />
    </van-nav-bar>
    <!-- 登录表单 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field v-model="user.mobile" :rules="loginFormRules.mobile" name="mobile"
        placeholder="请输入手机号" type="number" maxlength="11">
        <i slot="left-icon" class="iconfont icon-shouji"></i>
      </van-field>
      <van-field v-model="user.code" :rules="loginFormRules.code" name="code" placeholder="请输入验证码"
        type="number" maxlength="6">
        <i slot="left-icon" class="iconfont icon-yanzhengma"></i>
        <template #button>
          <!-- 倒计时组件 -->
          <van-count-down :time="1000 * 60" format="ss s后重试" v-if="isShowCountDown"
            @finish="isShowCountDown = false" />
          <van-button v-else size="small" native-type="button" round type="default" class="send-btn"
            @click="sendMsg">发送验证码
          </van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button block type="info">登&nbsp;录</van-button>
      </div>
    </van-form>
  </div>
</template>
<script>
import { login, sendMsg } from '@/api/user.js';
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: '13888888888',
        code: '246810',
      },
      loginFormRules: {
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式错误' },
        ],
      },
      isShowCountDown: false,
    };
  },
  created() {},
  computed: {},
  methods: {
    // async onSubmit() {
    onSubmit() {
      // 表单验证 (当表单提交的时候会自动触发表单验证)
      // toast提示
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true, // 禁用背景点击
        duration: 0, // 展示时长(ms)，值为 0 时，toast 不会消失
      });
      // 提交表单登录
      // try {
      //   const res = await login(this.user);
      //   console.log('登录成功', res);
      //   this.$store.commit('setUser', res);
      //   this.$toast.success('登录成功');
      // } catch (err) {
      //   if (err.response.status === 400) {
      //     console.log('手机号或验证码错误!', err);
      //     this.$toast.fail('手机号或验证码错误!');
      //   } else {
      //     console.log('登录失败!请稍后重试');
      //     this.$toast.fail('登录失败!请稍后重试!');
      //   }
      // }
      // 另一种写法
      login(this.user).then(res => {
        this.$store.commit('setUser', res);
        this.$router.back();
        this.$toast.success('登录成功');
      }).catch(err => {
        console.log(err);
        if (err.response.status === 400) {
          console.log('手机号或验证码错误!', err);
          this.$toast.fail('手机号或验证码错误!');
        } else {
          console.log('登录失败!请稍后重试');
          this.$toast.fail('登录失败!请稍后重试!');
        }
      })
    },
    // 发送验证码
    async sendMsg() {
      // 校验手机号
      try {
        await this.$refs.loginForm.validate('mobile');
        console.log('验证成功');
      } catch (error) {
        console.log('验证失败', error);
        return;
      }
      // 验证通过 显示倒计时
      this.isShowCountDown = true;
      // 发送验证码
      try {
        const res = await sendMsg(this.user.mobile);
        console.log('发送成功:', res);
        this.$toast('发送成功');
      } catch (error) {
        this.isShowCountDown = false;
        if (error.request.status == 429) {
          this.$toast('发送过于频繁！请稍后重试');
        } else {
          this.$toast('发送失败！请稍后重试');
        }
      }
    },
  },
};
</script>
<style lang='less' scoped>
.login-container {
  .iconfont {
    font-size: 37px;
    color: #666;
  }
  .send-btn {
    background-color: #ededed;
    color: #666666;
    font-size: 22px;
  }
  .van-count-down {
    color: #999;
  }
}
</style>
